package com.wonders.frame.controller;

import com.gcr.server.response.Response;
import com.wonders.frame.common.Enum.ResponseCodeEnum;
import com.wonders.frame.common.Enum.SessionKeyEnum;
import com.wonders.frame.common.QueryForm;
import com.wonders.frame.controller.core.CoreController;
import com.wonders.frame.entity.AuthAccount;
import com.wonders.frame.entity.AuthLog;
import com.wonders.frame.service.AuthLogService;
import com.wonders.frame.service.Core.CoreCurdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author qiu
 * @date 2020-05-13 12:28
 */
@RestController
@RequestMapping("/authLog")
public class AuthLogController extends CoreController<AuthLog> {
    Logger log = LoggerFactory.getLogger(com.wonders.frame.controller.AuthLogController.class);

    @Autowired
    private AuthLogService authLogService;

    @Override
    protected CoreCurdService getService() {
        return authLogService;
    }

    private final ReentrantLock lock = new ReentrantLock();

    @RequestMapping("getLists")
    public Response getListByParams(@RequestBody QueryForm form, HttpServletRequest request) {
        return getResponseWithoutMsg(getService().findAllByParams(form, request), ResponseCodeEnum.SUCCESS.getCode());
    }

    @RequestMapping("save")
    public Response save(@RequestBody AuthLog news, HttpServletRequest request) {
        try {
            lock.lock();
            log.info("request:authLog/save");
            log.info(news.toString());
            HttpSession session = request.getSession();
            AuthAccount acc = (AuthAccount) session.getAttribute(SessionKeyEnum.USERINFO.name());
            authLogService.save(news, acc == null ? "" : acc.getUser().getUserName());
            return successResponse(news);

        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        } finally {
            lock.unlock();
        }
    }


    @RequestMapping("clearLog")
    public Response clearLogByParams(@RequestBody QueryForm form, HttpServletRequest request) {
        try {
            Map<String, Object> params = form.getParams();
            String beginDate = Objects.nonNull(params.get("executeDate_gtFormatDateTime"))?String.valueOf(params.get("executeDate_gtFormatDateTime")):"";
            String endDate = Objects.nonNull(params.get("executeDate_ltFormatDateTime"))?String.valueOf(params.get("executeDate_ltFormatDateTime")):"";
            authLogService.deleteLog(beginDate, endDate);
            return getResponseWithoutData(0,ResponseCodeEnum.CLEARLOG_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return getResponseWithoutData(-1,ResponseCodeEnum.CLEARLOG_SUCCESS.getMsg());
        }
    }

}

